import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        aquariumId: '',
        success: true,
        show: false,

        items: [],
        checkedIndex: []
    },

    attached() {
        let userAquariumList = wx.getStorageSync('userAquariumList')
        let tempItems = []
        for (let i = 0; i < userAquariumList.length; i++) {
            let value = i
            let name = userAquariumList[i]
            let checked = false
            let item = { value, name, checked }
            tempItems.push(item)
        }

        this.setData({
            items: tempItems
        })
        console.log(this.data.items)
    },

    /**
     * 组件的方法列表
     */
    methods: {

        checkboxChange(e) {

            const items = this.data.items
            const values = e.detail.value

            let tempArr = []
            for (let i = 0; i < values.length; i++) {
                tempArr.push(Number(values[i]))
            }

            this.setData({
                checkedIndex: tempArr
            })
            // console.log(this.data.checkedIndex)
        },

        deleteBtn() {
            if (this.data.checkedIndex.length == 0) {
                wx.showToast({
                    title: '请选择要删除的id！',
                    icon: 'none',
                    duration: 1500
                })
                return
            }

            this.setData({
                show: true
            })
        },
        realDelete() {

            let username = wx.getStorageSync('username');
            let checkedIndex = this.data.checkedIndex;

            let aquariumIds = []
            for (let i = 0; i < checkedIndex.length; i++) {
                aquariumIds.push(this.data.items[checkedIndex[i]].name)
            }
            console.log(aquariumIds)

            wx.request({
                url: `http://120.77.156.205:8808/wx/ucenter/delUserAquarium/${username}`,
                data: {
                    aquariumIds: aquariumIds
                },
                method: 'POST',
                success: (res) => {
                    console.log(res)
                    if (res.data.code == 0) {
                        wx.showToast({
                            title: '删除成功！',
                            icon: 'success',
                        })
                        console.log(this)
                        this.updateAquariumIdList();
                    } else {
                        wx.showToast({
                            title: '删除失败！',
                            icon: 'error'
                        })
                    }
                },
                fail: (res) => {
                    console.log(res)
                    wx.showToast({
                        title: '删除失败！',
                        icon: 'error'
                    })
                }

            })



        },

        bindinput:function(e) {
            console.log("输入的数字为：",e.detail.value)
            this.setData({
                aquariumId: Number(e.detail.value)
            })
        },

        submit() {
            this.addAquarium(this.data.aquariumId)
        },

        scancode: function () {
            // 允许从相机和相册扫码
            let _this = this;
            wx.scanCode({
                success(res) {
                    console.log(res.result);  //从二维码中得到鱼缸id
                    _this.addAquarium(Number(res.result))

                },
                fail: (res) => {
                    console.log(res);
                    wx.showToast({
                        title: '扫码失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        },

        addAquarium(aquariumId) {
            console.log("鱼缸id为",aquariumId)
            if (!aquariumId) {
                wx.showToast({
                    title: '请扫码或输入id！',
                    icon: 'error',
                    duration: 2000
                })
            }
            let username = wx.getStorageSync("username")
            console.log(username)
            wx.request({
                url: `http://120.77.156.205:8808/wx/ucenter/add_or_restart/${aquariumId}/${username}`,
                method: 'GET',
                success: (res) => {
                    console.log(res)
                    if (res.data.code == 2) {
                        wx.showToast({
                            title: '输入的id不存在！',
                            duration: 2000,
                            icon: "error"
                        })
                    }
                    if (res.data.code == 1) {
                        this.setData({
                            success: false
                        })
                    }
                    if (res.data.code == 0) {
                        this.setData({
                            success: true
                        })
                        wx.showToast({
                            title: '添加成功！',
                            icon: 'success',
                            duration: 1000
                        })
                        console.log(this)
                        this.updateAquariumIdList()

                    }
                },
                fail: (res) => {
                    this.setData({
                        success: false
                    })
                }
            })
        },

        updateAquariumIdList() {
            console.log("更新列表")
            let username = wx.getStorageSync("username");
            wx.request({
                url: `http://120.77.156.205:8808/wx/aquarium/aquariumIdList/username?username=${username}`,
                method: 'GET',
                success: (res) => {

                    
                    if (res.data.code === 0) {
                        
                        //本地存储该用户所有鱼缸id
                        wx.setStorageSync('userAquariumList', res.data.data.userAquariumList)
                        //存储当前用户操作的鱼缸，先把索引为0的设为当前操作鱼缸
                        // wx.setStorageSync('curAquarium', res.data.data.userAquariumList[0])
                        //存储当前操作鱼缸的索引
                        wx.setStorageSync('curIndex', Number(0))
                        //用户有没有添加水族箱
                        wx.setStorageSync('exists', 'exists')


                        var userAquariumArray = []
                        // res.data.data.userAquariumList.forEach(function(item,index){
                        //     let temp = {name:item,curIndex:index}
                        //     userAquariumArray.push(temp)
                        // })

                        for (var index in res.data.data.userAquariumList) {
                            var temp = { name: res.data.data.userAquariumList[index], curIndex: Number(index) }

                            userAquariumArray.push(temp)
                        }

                        wx.setStorageSync('userAquariumArray', userAquariumArray)

                        wx.reLaunch({
                            url: '/pages/index/index',
                        })

                    }else if(res.data.code == 6){
                        console.log("xxxxxx")

                        wx.setStorageSync('userAquariumList', '')
                        
                        wx.setStorageSync('curIndex', '')
                        
                        wx.setStorageSync('exists', '')
                        wx.setStorageSync('userAquariumArray', '')
                        wx.reLaunch({
                            url: '/pages/index/index',
                        })
                    }

                }
            })
        }
    }
})
